Option Strict On
Option Explicit On

Imports System

Public Class XmlHelpers
   Public Shared Function GetAttributeOrEmptyString( _
               ByVal node As Xml.XmlNode, _
               ByVal ElementName As String, _
               ByVal attributeName As String, _
               ByVal nsmgr As Xml.XmlNamespaceManager) _
               As String
      Dim elem As Xml.XmlNode
      If ElementName Is Nothing OrElse ElementName.Trim.Length = 0 Then
         Return GetAttributeOrEmptyString(node, attributeName)
      Else
         elem = node.SelectSingleNode(ElementName, nsmgr)
         If elem Is Nothing Then
            Return ""
         Else
            Return GetAttributeOrEmptyString(elem, attributeName)
         End If
      End If
   End Function

   Public Shared Function GetAttributeOrEmptyString( _
                  ByVal node As Xml.XmlNode, _
                  ByVal attributeName As String) _
                  As String
      Dim ret As String = ""
      If Not node Is Nothing Then
         If Not node.Attributes Is Nothing Then
            Dim attr As Xml.XmlNode = node.Attributes.GetNamedItem(attributeName)
            If Not attr Is Nothing Then
               ret = attr.Value
            End If
         End If
      End If
      Return ret
   End Function

   Public Shared Function GetAttributeBooleanOrEmpty( _
              ByVal node As Xml.XmlNode, _
              ByVal ElementName As String, _
              ByVal attributeName As String, _
              ByVal nsmgr As Xml.XmlNamespaceManager) _
              As Nullable(Of Boolean)
      Dim elem As Xml.XmlNode
      If ElementName.Trim.Length = 0 Then
         Return GetAttributeBooleanOrEmpty(node, attributeName)
      Else
         elem = node.SelectSingleNode(ElementName, nsmgr)
         If elem Is Nothing Then
            Return Nothing
         Else
            Return GetAttributeBooleanOrEmpty(elem, attributeName)
         End If
      End If
   End Function

   Public Shared Function GetAttributeBooleanOrEmpty( _
                 ByVal node As Xml.XmlNode, _
                 ByVal attributeName As String) _
                 As Nullable(Of Boolean)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing Then
         Return Nothing
      ElseIf ret.Trim.ToLower = "true" Then
         Return True
      Else
         Return Nothing
      End If
   End Function

   Public Shared Function GetAttributeIntegerOrEmpty( _
                ByVal node As Xml.XmlNode, _
                ByVal attributeName As String) _
                As Nullable(Of Int32)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If String.IsNullOrEmpty(ret) Then
         Return Nothing
      Else
         Return Convert.ToInt32(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeByteOrEmpty( _
             ByVal node As Xml.XmlNode, _
             ByVal attributeName As String) _
             As Nullable(Of Byte)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If String.IsNullOrEmpty(ret) Then
         Return Nothing
      Else
         Return Convert.ToByte(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeSignedByteOrEmpty( _
            ByVal node As Xml.XmlNode, _
            ByVal attributeName As String) _
            As Nullable(Of SByte)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         Return Convert.ToSByte(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeUnsignedIntOrEmpty( _
              ByVal node As Xml.XmlNode, _
              ByVal attributeName As String) _
              As Nullable(Of UInt32)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         Return Convert.ToUInt32(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeUnsignedLongOrEmpty( _
             ByVal node As Xml.XmlNode, _
             ByVal attributeName As String) _
             As Nullable(Of UInt64)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         Return Convert.ToUInt64(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeLongOrEmpty( _
             ByVal node As Xml.XmlNode, _
             ByVal attributeName As String) _
             As Nullable(Of Int64)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         Return Convert.ToInt64(Microsoft.VisualBasic.Val(ret))
      End If
   End Function

   Public Shared Function GetAttributeFloatOrEmpty( _
                ByVal node As Xml.XmlNode, _
                ByVal attributeName As String) _
                As Nullable(Of Double)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         Return Microsoft.VisualBasic.Val(ret)
      End If
   End Function

   Public Shared Function GetAttributeDateOrEmpty( _
                ByVal node As Xml.XmlNode, _
                ByVal attributeName As String) _
                As Nullable(Of DateTime)
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Nothing
      Else
         If Microsoft.VisualBasic.IsDate(ret) Then
            Return CDate(ret)
         End If
      End If
   End Function

   Public Shared Function GetAttributeEnumOrEmpty( _
           ByVal enumType As System.Type, _
           ByVal node As Xml.XmlNode, _
           ByVal attributeName As String) _
           As System.Object
      Dim s As String = GetAttributeOrEmptyString(node, attributeName)
      If Not s.Trim.Length = 0 Then
         Return [Enum].Parse(enumType, s)
      End If
      Return Nothing
   End Function

   Public Shared Function GetAttributeGuidOrGuidEmpty( _
              ByVal node As Xml.XmlNode, _
              ByVal attributeName As String) _
              As Guid
      Dim ret As String = GetAttributeOrEmptyString(node, attributeName)
      If ret Is Nothing OrElse ret.Trim.Length = 0 Then
         Return Guid.Empty
      Else
         Return New Guid(ret)
      End If
   End Function

   Public Shared Sub WriteAttribute( _
            ByVal xmlWriter As Xml.XmlWriter, _
            ByVal name As String, _
            ByVal value As String)
      xmlWriter.WriteAttributeString(name, value)
   End Sub

   Public Shared Sub WriteAttribute( _
            ByVal xmlWriter As Xml.XmlWriter, _
            ByVal name As String, _
            ByVal value As Boolean)
      xmlWriter.WriteAttributeString(name, value.ToString().ToLower())
   End Sub

   Public Shared Sub WriteAttribute _
            (Of T As Structure) _
            (ByVal xmlWriter As Xml.XmlWriter, _
             ByVal name As String, _
             ByVal value As Nullable(Of T))
      Dim outputValue As String
      outputValue = value.GetValueOrDefault.ToString().ToLower()
      xmlWriter.WriteAttributeString(name, outputValue)
   End Sub

   Public Shared Sub WriteAttribute( _
            ByVal xmlWriter As Xml.XmlWriter, _
            ByVal name As String, _
            ByVal value As Object)
      xmlWriter.WriteAttributeString(name, value.ToString())
   End Sub

End Class
